package com.zhongli.devplatform.service.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.exception.ServiceException;
import com.zhongli.devplatform.mapper.sys.ReadTopicMapper;
import com.zhongli.devplatform.po.sys.ReadChapterExercisesPO;
import com.zhongli.devplatform.po.sys.ReadChapterExercisesTopicPO;
import com.zhongli.devplatform.po.sys.ReadTopicPO;
import com.zhongli.devplatform.service.imp.sys.ReadChapterExercisesService;
import com.zhongli.devplatform.service.imp.sys.ReadChapterExercisesTopicService;
import com.zhongli.devplatform.service.imp.sys.ReadTopicService;
import com.zhongli.devplatform.vo.sys.ChapterExercisesVO;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author wy
 * @since 2020/7/31
 */
@Service
public class ReadTopicServiceImpl extends ServiceImpl<ReadTopicMapper, ReadTopicPO> implements ReadTopicService {

    @Resource
    private ReadTopicMapper readTopicMapper;

    @Resource
    private ReadChapterExercisesService chapterExercisesService;

    @Resource
    private ReadChapterExercisesTopicService exercisesTopicService;

    /**
     * 进入练习题 创建章节练习 保存章节练习的所有题目
     * @param id 知识点id
     * @return 练习题集合 {@link ChapterExercisesVO} 返回章节练习id 和 练习题信息
     */
    @Override
    public ChapterExercisesVO createChapterExercises(Integer id) {
        ChapterExercisesVO chapterExercisesVO = new ChapterExercisesVO();//result
        //检查已有练习不允许在进行练习
        ReadChapterExercisesPO chapterExercisesPO = chapterExercisesService.getOne(new QueryWrapper<ReadChapterExercisesPO>()
                .eq("knowledge_id", id)
                .eq("student_id", SecurityHelper.getCurrentUserId()));
        List<ReadTopicPO> data = readTopicMapper.selectList(new QueryWrapper<ReadTopicPO>().eq("knowledge_id", id));
        if (data.isEmpty())  return chapterExercisesVO; //没有练习题就 返回空对象
        if (chapterExercisesPO != null) {
            chapterExercisesPO.setStartTime(new Date());
            chapterExercisesPO.setEndTime(null);
            chapterExercisesPO.setCorrectNum(null);
            chapterExercisesPO.setCorrectRate(null);
            chapterExercisesPO.setErrorNum(null);
            chapterExercisesService.updateById(chapterExercisesPO);
            data.forEach(topic->{
                ReadChapterExercisesTopicPO exercisesTopicPO = new ReadChapterExercisesTopicPO();
                exercisesTopicPO.setChapterExercisesId(chapterExercisesPO.getId());
                exercisesTopicPO.setTopicId(topic.getId());
                exercisesTopicPO.setCorrectAnswer(topic.getCorrectAnswer());
                exercisesTopicPO.setStatus(null);
                exercisesTopicPO.setCommitAnswer(null);
                UpdateWrapper<ReadChapterExercisesTopicPO> update = new UpdateWrapper<>();
                update.eq("chapter_exercises_id",chapterExercisesPO.getId()).eq("topic_id",topic.getId());
                exercisesTopicService.update(exercisesTopicPO,update);
            });
            chapterExercisesVO.setChapterExercisesId(chapterExercisesPO.getId());
        }else {
            //保存章节练习
            ReadChapterExercisesPO exercisesPO = new ReadChapterExercisesPO();
            exercisesPO.setStudentId(SecurityHelper.getCurrentUserId());
            exercisesPO.setStartTime(new Date());
            exercisesPO.setKnowledgeId(id);
            chapterExercisesService.save(exercisesPO);
            //保存章节练习 所包含的每一道练习题
            ArrayList<ReadChapterExercisesTopicPO> exercisesTopicList = new ArrayList<>();
            data.forEach(topic->{
                ReadChapterExercisesTopicPO exercisesTopicPO = new ReadChapterExercisesTopicPO();
                exercisesTopicPO.setChapterExercisesId(exercisesPO.getId());
                exercisesTopicPO.setTopicId(topic.getId());
                exercisesTopicPO.setCorrectAnswer(topic.getCorrectAnswer());
                exercisesTopicList.add(exercisesTopicPO);
            });
            exercisesTopicService.saveBatch(exercisesTopicList);
            chapterExercisesVO.setChapterExercisesId(exercisesPO.getId());
        }
        chapterExercisesVO.setTopics(data);
        return chapterExercisesVO;

    }
}
